<template>
  <div>
    <el-row>
      <el-tree
        :data="data"
        :props="defaultProps"
        :highlight-current="true"
        :default-expand-all="true"
        @node-click="handleNodeClick"
      ></el-tree>
    </el-row>
  </div>
</template>

<script>
  import { loadProDeviceTree } from '@/api/Factorymodel'

  export default {
    name: 'RoleManagementEdit',
    data() {
      return {
        currtreenode: '',
        data: [],
        defaultProps: {
          children: 'children',
          label: 'label',
          id: '0',
        },
        form: {
          id: '',
          code: '',
          birthday: '',
          enable: '1',
        },
        rules: {},
        title: '',
        dialogFormVisible1: false,
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleNodeClick(data) {
        //console.log(data)
        //this.currtreenode = data
        this.$emit('fetch-data', data)
      },
      showEdit(flag) {
        this.title = '编辑'
        if (flag == 'edit') {
          if (this.currtreenode == '') {
            this.$baseMessage('请选机构节点', 'error')
            return false
          }
          this.form = Object.assign(
            {},
            {
              id: this.currtreenode.id,
              code: this.currtreenode.nodecode,
              deptname: this.currtreenode.label,
              parentid: this.currtreenode.parentid,
              enable: this.currtreenode.enable,
            }
          )
        } else {
          this.form = Object.assign(
            {},
            {
              id: '',
              code: '',
              deptname: '',
              parentid: this.currtreenode == '' ? '' : this.currtreenode.id,
              enable: '1',
            }
          )
        }
        this.dialogFormVisible1 = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible1 = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveDept(this.form)
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
            this.fetchData()
          } else {
            return false
          }
        })
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await loadProDeviceTree(this.queryForm)
        this.data = data
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
